<?php
require('config.php');
        $user_id=$_REQUEST['user_id'];
        //$user_id=9;
        $start=0;
        $num=10;
        $isWb=1;
        if(isset($_REQUEST['start'])) $start=$_REQUEST['start'];
        if(isset($_REQUEST['num'])) $num=$_REQUEST['num'];
        if(isset($_REQUEST['isWb'])) $isWb=$_REQUEST['isWb'];
		// $sql="select g.goodsNums,g.goodsPrice,g.goodsAttrName,g.goodsThums,g.goodsId,g.goodsName,o.needPay,o.orderStatus,o.orderunique,o.orderId,o.isRefund,s.shopId,s.shopName,s.shopImg,o.createTime as xiadan_time,o.isPay from `".$oto."_orders` as o,`".$oto."_order_goods` as g,`".$oto."_shops` as s where o.userId='".$user_id."' and `orderFlag`='1' and o.orderId=g.orderId and s.shopId=o.shopId and o.orderType in ('1','2','3') and (o.orderStatus in ('-7','-6','-4','-3','-2','0','1','2','3','4','5','6','7','8') or (o.orderStatus='-1' and o.isRefund='2')) order by o.createTime DESC";
        $sql="select g.goodsNums,g.goodsPrice,g.goodsAttrName,g.goodsThums,g.goodsId,g.goodsName,o.needPay,o.orderStatus,o.orderunique,o.orderId,o.isRefund,s.shopId,s.shopName,s.shopImg,o.createTime as xiadan_time,o.isPay from `".$oto."_orders` as o,`".$oto."_order_goods` as g,`".$oto."_shops` as s where o.userId='".$user_id."' and o.orderId=g.orderId and s.shopId=o.shopId order by o.createTime DESC";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$orders[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$t=0;
		if(isset($orders))
		{
			for($i=0;$i<count($orders);$i++)
			{
				$orderId=$orders[$i]['orderId'];
				$sql="SELECT * FROM ".$oto."_goods_appraises where orderId='{$orderId}'";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$pingjia[$i][]=$row;//将取得的所有数据赋值给person_info数组
				}
				if(isset($pingjia[$i]))
				{
					$orders[$i]['isPingjia']=1;
				}
				else $orders[$i]['isPingjia']=0;
			}
			$pan_tiao=0;
			for($i=0;$i<count($orders);$i++)
			{
				if($orders[$i]['orderunique']!=$pan_tiao)
				{
					$data[$t]['orderId']=$orders[$i]['orderId'];
					$data[$t]['shopName']=$orders[$i]['shopName'];
					$data[$t]['shopImg']=$orders[$i]['shopImg'];
					$data[$t]['shopId']=$orders[$i]['shopId'];
					$data[$t]['needPay']=$orders[$i]['needPay'];
					$data[$t]['orderStatus']=$orders[$i]['orderStatus'];
					$data[$t]['xiadan_time']=$orders[$i]['xiadan_time'];
					$data[$t]['isRefund']=$orders[$i]['isRefund'];
					$data[$t]['isPingjia']=$orders[$i]['isPingjia'];
					$data[$t]['isPay']=$orders[$i]['isPay'];
					$data[$t]['dd_type']="yungou";
					if($orders[$i]['orderStatus']=='-2') $data[$t]['msg']="等待付款";
					if($orders[$i]['orderStatus']=='0' || $orders[$i]['orderStatus']=='1' || $orders[$i]['orderStatus']=='2' || $orders[$i]['orderStatus']=='5') $data[$t]['msg']="等待发货";
					if($orders[$i]['orderStatus']=='3' || $orders[$i]['orderStatus']=='6' || $orders[$i]['orderStatus']=='7') $data[$t]['msg']="等待收货";
					if($orders[$i]['orderStatus']=='8') $data[$t]['msg']="已完成";
					if($orders[$i]['orderStatus']=='-7' || $orders[$i]['orderStatus']=='-6' || $orders[$i]['orderStatus']=='-3' || $orders[$i]['orderStatus']=='-1' || $orders[$i]['orderStatus']=='-4'){
						if($orders[$i]['isRefund']==2) {
							$data[$t]['msg']="退款成功";
						}else {
							$data[$t]['msg']="退款中";
						}
						if ($orders[$i]['orderStatus'] == '-1'){
							$data[$t]['msg']="已取消";
						}
					}
					$t2=0;
					$t2_sum=0;
					for($j=0;$j<count($orders);$j++)
					{
						if($orders[$i]['orderunique']==$orders[$j]['orderunique'])
						{
							$data[$t]['list'][$t2]['goodsId']=$orders[$j]['goodsId'];
							$data[$t]['list'][$t2]['goodsName']=$orders[$j]['goodsName'];
							$data[$t]['list'][$t2]['goodsThums']=$orders[$j]['goodsThums'];
							$data[$t]['list'][$t2]['goodsNums']=$orders[$j]['goodsNums'];
							$data[$t]['list'][$t2]['goodsAttrName']=$orders[$j]['goodsAttrName'];
							$data[$t]['list'][$t2]['goodsPrice']=$orders[$j]['goodsPrice'];
							$t2_sum+=$orders[$j]['goodsNums'];
							$t2++;
						}
					}
					$data[$t]['num']=$t2_sum;
					if($t2==0) $data[$t]['list']=[];
					$t++;
					$pan_tiao=$orders[$i]['orderunique'];
				}
			}
		}
		$sql="select o.*,h.title,h.content,h.type from ".$oto."_help_orders as o,".$oto."_help as h where o.helpId=h.id and o.isDel='0' and o.orderStatus!='-1' and (o.userId='$user_id' or o.acceptUserId='$user_id') order by o.createTime DESC";
		//file_put_contents("tsxx.txt", "\r\n微帮订单列表：".$sql."\r\n", FILE_APPEND);
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$wb_orders[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(isset($wb_orders) && $isWb==1)
		{
			for($i=0;$i<count($wb_orders);$i++)
			{
				if($user_id==$wb_orders[$i]['userId']) $lin_user_id=$wb_orders[$i]['acceptUserId'];
				if($user_id==$wb_orders[$i]['acceptUserId']) $lin_user_id=$wb_orders[$i]['userId'];
				$sql="select userPhoto,userName from ".$oto."_users where userId='{$lin_user_id}'";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$users[$i][]=$row;//将取得的所有数据赋值给person_info数组
				}
				if(isset($users[$i]))
				{
					$wb_orders[$i]['userPhoto']=$users[$i][0]['userPhoto'];
					$wb_orders[$i]['userName']=$users[$i][0]['userName'];
				}
				else
				{
					$wb_orders[$i]['userPhoto']='';
					$wb_orders[$i]['userName']='';
				}
				if($wb_orders[$i]['type']==0 && $wb_orders[$i]['userId']==$user_id)
				{
					$wb_orders[$i]['zt']=1;//需求方-发布方
				}
				if($wb_orders[$i]['type']==0 && $wb_orders[$i]['acceptUserId']==$user_id)
				{
					$wb_orders[$i]['zt']=2;//需求方-接单方
				}
				if($wb_orders[$i]['type']==1 && $wb_orders[$i]['userId']==$user_id)
				{
					$wb_orders[$i]['zt']=3;//服务方-发布方
				}
				if($wb_orders[$i]['type']==1 && $wb_orders[$i]['acceptUserId']==$user_id)
				{
					$wb_orders[$i]['zt']=4;//服务方-接单方
				}
				//file_put_contents("tsxx.txt", "\r\n微帮zt：".$wb_orders[$i]['type'].",".$user_id.",".$wb_orders[$i]['userId'].",".$wb_orders[$i]['acceptUserId'].",".."\r\n", FILE_APPEND);
				$data[$t]['userId']=$lin_user_id;
				$data[$t]['userPhoto']=$wb_orders[$i]['userPhoto'];
				$data[$t]['userName']=$wb_orders[$i]['userName'];
				$data[$t]['title']=$wb_orders[$i]['title'];
				$data[$t]['content']=$wb_orders[$i]['content'];
				$data[$t]['type']=$wb_orders[$i]['type'];
				$data[$t]['dd_type']="weibang";
				$data[$t]['zt']=$wb_orders[$i]['zt'];
				$data[$t]['helpId']=$wb_orders[$i]['helpId'];
				$data[$t]['orderId']=$wb_orders[$i]['id'];
				$data[$t]['isPay']=$wb_orders[$i]['isPay'];
				$data[$t]['orderStatus']=$wb_orders[$i]['orderStatus'];
				$data[$t]['needPay']=$wb_orders[$i]['money'];
				$data[$t]['isRefund']=$wb_orders[$i]['isRefund'];
				$data[$t]['xiadan_time']=$wb_orders[$i]['createTime'];
				$data[$t]['msg']="服务状态";
				if($wb_orders[$i]['orderStatus']==0) $data[$t]['msg']="订单已提交";
				if($wb_orders[$i]['orderStatus']==1)
				{
					if($wb_orders[$i]['zt']==1 || $wb_orders[$i]['zt']==4)
					{
						if($wb_orders[$i]['isRefund']==3) $data[$t]['msg']="对方拒绝了您的退款申请";
						else $data[$t]['msg']="接单者已接单";
					}
					else $data[$t]['msg']="接单者已接单";
				}
				if($wb_orders[$i]['orderStatus']==2)
				{
					if($wb_orders[$i]['zt']==1 || $wb_orders[$i]['zt']==4)
					{
						if($wb_orders[$i]['isRefund']==3) $data[$t]['msg']="对方拒绝了您的退款申请";
						else $data[$t]['msg']="接单者已完成";
					}
					else $data[$t]['msg']="接单者已完成";
				}
				if($wb_orders[$i]['orderStatus']==3) $data[$t]['msg']="订单已完成";
				if($wb_orders[$i]['orderStatus']==-1)
				{
					if($wb_orders[$i]['zt']==1 || $wb_orders[$i]['zt']==4) $data[$t]['msg']="对方已取消订单";
					else $data[$t]['msg']="已取消订单";
				}
				if($wb_orders[$i]['orderStatus']==-2)
				{
					if($wb_orders[$i]['zt']==1 || $wb_orders[$i]['zt']==4) $data[$t]['msg']="对方已取消订单,已退款至账户";
					else $data[$t]['msg']="已取消订单";
				}
				if($wb_orders[$i]['orderStatus']==-3)
				{
					if($wb_orders[$i]['zt']==1 || $wb_orders[$i]['zt']==4)
					{
						if($wb_orders[$i]['isRefund']==1) $data[$t]['msg']="退款申请中";
					} 
					else $data[$t]['msg']="对方申请退款";
				}
				if($wb_orders[$i]['orderStatus']==-4)
				{
					if($wb_orders[$i]['zt']==1 || $wb_orders[$i]['zt']==4)
					{
						if($wb_orders[$i]['isRefund']==1) $data[$t]['msg']="退款申请中";
					} 
					else $data[$t]['msg']="对方申请退款";
				}
				if($wb_orders[$i]['orderStatus']==-5)
				{
					if($wb_orders[$i]['zt']==1 || $wb_orders[$i]['zt']==4)
					{
						if($wb_orders[$i]['isRefund']==2) $data[$t]['msg']="退款成功";
					}
					else
					{
						if($wb_orders[$i]['isRefund']==2) $data[$t]['msg']="退款成功";
					}
				}
				if($wb_orders[$i]['orderStatus']==-6) $data[$t]['msg']="退款成功";
				if($wb_orders[$i]['orderStatus']==-7 || $wb_orders[$i]['orderStatus']==-8) $data[$t]['msg']="等待平台处理";
				if(($wb_orders[$i]['isPay']==0 || $wb_orders[$i]['isPay']=='0') && $wb_orders[$i]['orderStatus']==0)
				{
					if($wb_orders[$i]['zt']==1 || $wb_orders[$i]['zt']==4)
					{
						$data[$t]['msg']="未付款";
					}
					else $data[$t]['msg']="等待对方付款";
				} 
				$t++;
			}
		}
		if($t>0)
		{
			for($i=0;$i<count($data);$i++)
			{
				for($j=$i;$j<count($data);$j++)
				{
					if($data[$i]['xiadan_time']<$data[$j]['xiadan_time'])
					{
						$lin=$data[$i];
						$data[$i]=$data[$j];
						$data[$j]=$lin;
					}
				}
			}
			$tt=0;
			for($i=$start;$i<count($data);$i++)
			{
				if($i>=$start+$num) break;
				$data2[$tt]=$data[$i];
				$tt++;
			}
			if(isset($data2)) echo json_encode($data2);
			else echo '[]';
		} 
		else echo '[]';
?>